package com.kkb.javawebtest.DBUtils;

import com.alibaba.druid.pool.DruidDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author whl
 * @date 2021/8/25 @11:11
 */
public class DBUtils {
    private Connection connection;
    private PreparedStatement preparedStatement;
    private ResultSet resultSet;
    private int count;

    private static String userName;
    private static String userPassword;
    private static String url;
    private static String driverName;
    //德鲁伊
    private static DruidDataSource dataSource =new DruidDataSource();
    static {
        //读取到db文件的配置
        ResourceBundle bundle = ResourceBundle.getBundle("db");
        driverName = bundle.getString("driverclass");
        url = bundle.getString("url");
        userName = bundle.getString("uname");
        userPassword = bundle.getString("upass");

        //
        dataSource.setUsername(userName);
        dataSource.setPassword(userPassword);
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driverName);


    }
    //连接
    public Connection getConnection(){
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
    //得到预状态通道
    public PreparedStatement getPreparedStatement(String sql){
        try {
            preparedStatement = getConnection().prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return preparedStatement;

    }
    //绑定参数 List保存的是给占位符所赋的值,因为可能有多个放在集合里
    public void param(List list){

        if (list != null&&list.size()>0) {
            for (int i = 0; i < list.size(); i++) {
                try {
                    preparedStatement.setObject(i+1,list.get(i));
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //查询返回结果集
    public ResultSet query(String sql,List list){
        getPreparedStatement(sql);
        param(list);
        try {
            resultSet = preparedStatement.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return resultSet;
    }
    //增删改 返回影响的行数
    public int update(String sql,List list){
        getPreparedStatement(sql);
        param(list);
        try {
            count = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
    //关闭所有资源
    public void closedAll(){
        try {
            if (connection != null) {
                connection.close();
            }

            if (preparedStatement != null) {
                preparedStatement.close();

            }
            if (resultSet != null) {
                resultSet.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
